package com.etung.iot.demo.handler;

import com.etung.iot.demo.dao.UserMapper;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

@Component
public class UserCacheUtil {
    @Autowired
    private UserMapper userMapper;
    private Map<String,String> userMap = Maps.newConcurrentMap();

    public String getUid(String imei){
        String userId = "";
        if(!userMap.containsKey(imei)){
            userId = userMapper.selectUserByImei(imei);
            if (!StringUtils.isEmpty(userId)){
                userMap.put(imei,userId);
            }
        }else {
            userId = userMap.get(imei);
        }
        return userId;
    }

    public void removeUserId(String imei){
        userMap.entrySet().removeIf(entry -> entry.getKey().equals(imei));
    }
}
